{
  Copyright 2012 Sergey Ostanin

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
}

unit Questions;

interface

uses
  Classes, SysUtils, MiscUtils;

type
  TQuestionRegistry = class
  private type
    TEntry = class
    private
      FKind: TKind;
      FTitle: String;
      FIconResourceName: String;
      FInitialText: String;
    public
      constructor Create(Kind: TKind; const Title, IconResourceName, InitialText: String);
    end;
    TEntryList = TGenericObjectList<TEntry>;
  private
    FEntries: TEntryList;
    function GetCount: Integer;
    procedure CheckIndex(Index: Integer);
  public
    constructor Create;
    destructor Destroy; override;
    procedure Add(Kind: TKind; const Title, IconResourceName, InitialText: String);
    function Find(Kind: TKind): Integer;
    function GetIconResourceName(Index: Integer): String;
    function GetTitle(Index: Integer): String;
    function GetKind(Index: Integer): TKind;
    function GetInitialText(Index: Integer): String;

    property Count: Integer read GetCount;
  end;

function QuestionRegistry: TQuestionRegistry;

implementation

var
  Registry: TQuestionRegistry;

function QuestionRegistry: TQuestionRegistry;
begin
  Result := Registry;
end;

{ TQuestionRegistry.TEntry }

constructor TQuestionRegistry.TEntry.Create(Kind: TKind; const Title,
  IconResourceName, InitialText: String);
begin
  inherited Create;
  FKind := Kind;
  FTitle := Title;
  FIconResourceName := IconResourceName;
  FInitialText := InitialText;
end;

{ TQuestionRegistry }

function TQuestionRegistry.GetCount: Integer;
begin
  Result := FEntries.Count;
end;

procedure TQuestionRegistry.CheckIndex(Index: Integer);
begin
  Assert( Index >= 0 );
  Assert( Index < FEntries.Count );
end;

constructor TQuestionRegistry.Create;
begin
  inherited;
  FEntries := TEntryList.Create;
end;

destructor TQuestionRegistry.Destroy;
begin
  FreeAndNil(FEntries);
  inherited;
end;

procedure TQuestionRegistry.Add(Kind: TKind; const Title,
  IconResourceName, InitialText: String);
begin
  FEntries.AddSafely(TEntry.Create(Kind, Title, IconResourceName, InitialText));
end;

function TQuestionRegistry.Find(Kind: TKind): Integer;
begin
  for Result := 0 to FEntries.Count-1 do
    if FEntries[Result].FKind = Kind then
      Exit;
  raise Exception.CreateFmt('Question %s not found.', [KindToString(Kind)]);
end;

function TQuestionRegistry.GetIconResourceName(Index: Integer): String;
begin
  CheckIndex(Index);
  Result := FEntries[Index].FIconResourceName;
end;

function TQuestionRegistry.GetTitle(Index: Integer): String;
begin
  CheckIndex(Index);
  Result := FEntries[Index].FTitle;
end;

function TQuestionRegistry.GetKind(Index: Integer): TKind;
begin
  CheckIndex(Index);
  Result := FEntries[Index].FKind;
end;

function TQuestionRegistry.GetInitialText(Index: Integer): String;
begin
  CheckIndex(Index);
  Result := FEntries[Index].FInitialText;
end;

initialization

  Registry := TQuestionRegistry.Create;

finalization

  FreeAndNil(Registry);

end.

